package com.sanjiahantang.ythshopping.adapter.home;

import android.content.Context;
import android.content.Intent;
import android.graphics.Paint;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.TextView;

import com.sanjiahantang.ythshopping.R;
import com.sanjiahantang.ythshopping.activity.GoodsInfoActivity;
import com.sanjiahantang.ythshopping.config.ConfigValue;
import com.sanjiahantang.ythshopping.model.BestNewHot;
import com.squareup.picasso.Picasso;

import java.util.List;


/**
 * Created by Administrator on 2017/6/8.
 * 精品推荐适配器
 */

public class GvJptjAdapter extends BaseAdapter {
    private Context context;
    private LayoutInflater inflater;

    private List<BestNewHot> datas;

    public GvJptjAdapter(Context context, List<BestNewHot> datas) {
        this.context = context;
        inflater = LayoutInflater.from(context);
        this.datas = datas;
    }

    @Override
    public int getCount() {
        return datas.size();
    }

    @Override
    public Object getItem(int i) {
        return null;
    }

    @Override
    public long getItemId(int i) {
        return i;
    }

    @Override
    public View getView(final int i, View view, ViewGroup viewGroup) {
        ViewHolder holder = null;
        if (view == null) {
            view = inflater.inflate(R.layout.item_recommend, null);
            holder = new ViewHolder(view);
            view.setTag(holder);
        } else {
            holder = (ViewHolder) view.getTag();
        }

        //绑定数据
        holder.bindData(datas.get(i));
        view.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent intent = new Intent(context, GoodsInfoActivity.class);
                Bundle bundle = new Bundle();
                bundle.putString("goodsId", datas.get(i).getGoods_id());
                intent.putExtras(bundle);
                context.startActivity(intent);
            }
        });

        return view;
    }

    class ViewHolder implements View.OnClickListener {
        private ImageView iv_goodsIcon;
        private TextView tv_goodsInfo, tv_nowPrice, tv_beforePrice;

        public ViewHolder(View view) {
            iv_goodsIcon = (ImageView) view.findViewById(R.id.iv_goodsIcon);
//            iv_cartIcon = (ImageView) view.findViewById(R.id.iv_cartIcon);
            tv_goodsInfo = (TextView) view.findViewById(R.id.tv_goodsInfo);
            tv_nowPrice = (TextView) view.findViewById(R.id.tv_nowPrice);
            tv_beforePrice = (TextView) view.findViewById(R.id.tv_beforePrice);
        }

        /**
         * 绑定数据
         *
         * @param bestNewHot
         */
        public void bindData(BestNewHot bestNewHot) {

            Picasso.with(context).load(ConfigValue.IMG_IP + bestNewHot.getGoods_img()).into(iv_goodsIcon);

            tv_goodsInfo.setText(bestNewHot.getGoods_name());
            tv_nowPrice.setText(bestNewHot.getShop_price());
            tv_beforePrice.setText(bestNewHot.getMarket_price());
            tv_beforePrice.getPaint().setFlags(Paint.STRIKE_THRU_TEXT_FLAG);

//            iv_cartIcon.setOnClickListener(this);
        }

        @Override
        public void onClick(View view) {
            switch (view.getId()) {
//                case R.id.iv_cartIcon:
//                    //加入购物车操作
//                    break;
            }
        }


    }
    public void AddDate(List<BestNewHot> addlist)
    {
        datas.addAll(addlist);
        notifyDataSetChanged();
    }
}
